let request = require('operation.js')
let bxt = require('../utils/bxt.js')

module.exports.updateReceipt = updateReceipt

/**
 * params {}
 * 根据不同的情况传入不同数据
 */
function updateReceipt(source,receiptSid, params) {
  let that = source

  that.setButtonLoading(true)

  return new Promise((resolve, reject) => {
    request.startRequest('PUT', '/receipts/' + receiptSid, params, true)
      .then(res => {
        that.setButtonLoading(false)
        let data = res.data
        if (request.SUCCESSED == data.status) {
          wx.showToast({
            title: '重签成功',
            icon: "success"
          })
          // 更新内存中保留的签收数据,为了让通知详情、签收学生选择、通知列表界面刷新界面
          let receiptObject = data.object
          getApp().notificationCenter.post(bxt.UPDATE_RECEIPT_SUCCESSED_MESSAGE, { receiptObject })

          resolve(receiptObject)
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
      })
      .catch(e => {
        that.setButtonLoading(false)
        wx.showToast({
          title: e,
          icon: 'none'
        })
      })
  })

}